package com.apes.framework.plugin.condition.model;

import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：条件字段目录- 条件字段的集合，也即条件表的关键字段，决定在不同层次上的定价
 *
 * @author xul
 * @create 2018-11-06 17:19
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FieldCatalog.CONST_TABLE_NAME)
public class FieldCatalog implements ApesBean {
    public static final String CONST_TABLE_NAME = "tech_field_catalog";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "条件字段目录")
    @Comment("编号")
    private String id;

    @Column(name = "name", nullable = false, length = 120)
    @Comment("描述")
    private String name;

    @OneToMany(mappedBy = "fieldCatalog", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<FieldCatalogItem> fieldCatalogItems = new HashSet();

    public Set<FieldCatalogItem> getFieldCatalogItems() {
        for (FieldCatalogItem item : fieldCatalogItems) {
            item.setFieldCatalog(this);
        }
        return fieldCatalogItems;
    }
}
